// StringUtils.cpp

#include "StdAfx.h"

#include "StringUtils.h"

namespace Common
{
namespace String
{

void SplitStringToTwoStrings(const CString &src, CString &dest1, CString &dest2)
{
  dest1.Empty();
  dest2.Empty();
  bool quoteMode = false;
  int i;
  for (i = 0; i < src.GetLength(); i++)
  {
    TCHAR c = src[i];
    if (_T('\"') == c)
      quoteMode = !quoteMode;
    else if (_T(' ') == c && !quoteMode)
    {
//      if (!quoteMode)
      {
        i++;
        break;
      }
    }
    else
      dest1 += c;
  }
  dest2 = src.Mid(i);
}

void SplitStringBySpace(const CString &srcString, CStringVector &destStrings)
{
  destStrings.clear();
  CString string;
  int len = srcString.GetLength();
  if (len == 0)
    return;
  for (int i = 0; i < len; i++)
  {
    wchar_t c = srcString[i];
    if (c == _T(' '))
    {
      if (!string.IsEmpty())
      {
		  destStrings.push_back(string);
        string.Empty();
      }
    }
    else
      string += c;
  }
  if (!string.IsEmpty())
	  destStrings.push_back(string);
}

CString JoinStringsByAddingSpace(const CStringVector &srcStrings)
{
  CString destString;
  for (size_t i = 0; i < srcStrings.size(); i++)
  {
    if (i != 0)
      destString += L' ';
    destString += srcStrings[i];
  }
  return destString;
}

}
}